package com.example.springaop.aop.myaop;



import com.example.springaop.aop.annotation.UserAnnotation;
import com.example.springaop.aop.dao.UserDao;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Component
@Aspect
public class AnnotationAopAspect {

    @Autowired
    private UserDao userDao;

    @Pointcut("@annotation(com.example.springaop.aop.annotation.UserAnnotation)")
    public void UserAopAspect() {
    }

    @Before("UserAopAspect()")
    public void doBefore(JoinPoint joinPoint){
        //1.获取切入点所在目标对象
        Object targetObj =joinPoint.getTarget();
        System.out.println("切入点所在目标对象:"+targetObj.getClass().getName());
        // 2.获取切入点方法的名字
        String methodName = joinPoint.getSignature().getName();
        System.out.println("切入方法名字："+methodName);
        System.out.println("切入方法的Signature："+joinPoint.getSignature());
        // 3. 获取方法上的注解
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();

        if (method != null)
        {
            UserAnnotation userAnnotation=  method.getAnnotation(UserAnnotation.class);
            System.out.println("切入方法注解的method:"+userAnnotation.method());
            System.out.println("切入方法注解的module:"+userAnnotation.module());
        }

        //4. 获取方法的参数
        Object[] args = joinPoint.getArgs();
        for(Object o :args){
            System.out.println("切入方法的参数："+o);
        }
        userDao.test();
        System.out.println("doBefore");

    }

    /*@After("UserAopAspect()")
    public void doAfter(JoinPoint joinPoint){
        System.out.println("doAfter");
    }

    @AfterReturning("UserAopAspect()")
    public void doAfterReturning(JoinPoint joinPoint){
        System.out.println("doAfterReturning");
    }

    @AfterThrowing("UserAopAspect()")
    public void deAfterThrowing(JoinPoint joinPoint){
        System.out.println("deAfterThrowing");
    }

    @Around("UserAopAspect()")
    public Object deAround(ProceedingJoinPoint joinPoint) throws Throwable{
        System.out.println("deAround方法执行前");
        Object proceed = joinPoint.proceed();
        System.out.println("deAround方法执行后");
        return proceed;
    }*/
}
